#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Create by Albert_Chen
# CopyRight (py) 2016年 陈超. All rights reserved by Chao.Chen.
# Create on 2016-08-15

__author__ = 'Albert'

import requests
import json
from flask import Flask
from flask import request, render_template, abort

app = Flask(__name__)

remote = 'http://114.55.117.203'
api = '/cbi360/company/search'

@app.route('/', methods=['GET'])
def index():

    query = request.args.get("query") or "" #查询的语句
    page_index = request.args.get('pageIndex') or "1" # 默认为第一页
    mode = request.args.get('mode') or '1'

    params = dict(query=query, pageIndex=page_index, mode=mode)
    url = remote + api

    app.logger.info('request data from %s, method get' %url)
    resp = requests.request('get', url=url, params=params)

    if resp.status_code == 200:
        data = json.loads(resp.content)
        data = data['data']
        count = data.get('count')
        hints = data.get('hits')
        page_count = (int(count)/ 20 + 1) if (int(count) % 20 > 0) else int(count)/20
        return render_template('index.html',
                               count=count,
                               hints=hints,
                               pageCount=page_count,
                               pageIndex=page_index,
                               query=query,
                               mode=mode)
    else:
        abort(404)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404



if __name__ == "__main__":

    app.run(debug=True, port=8080)